package dataobjects;

import utilities.Constants;

public class Edge {
	private Position pos0;
	private Position pos1;

	public Edge(Position pos0, Position pos1){
		this.pos0 = pos0;
		this.pos1 = pos1;

	}

	public Position getPos0() {
		return pos0;
	}

	public void setPos0(Position pos0) {
		this.pos0 = pos0;
	}

	public Position getPos1() {
		return pos1;
	}

	public void setPos1(Position pos1) {
		this.pos1 = pos1;
	}



	@Override
	public int hashCode(){ //derived from python get Edge index
		int x0 = pos0.getX();
		int x1 = pos1.getX();
		int y0 = pos0.getY();
		int y1 = pos1.getY();
		int index = 0;
		if (x0 == x1){
			if (y0 < y1){
				index = (y0 + 1) * Constants.GRID_WIDTH + y0 * (Constants.GRID_WIDTH+1) + x0;
			} else {
				index = (y1 + 1) * Constants.GRID_WIDTH + y1 * (Constants.GRID_WIDTH+1) + x0;
			}
		} else if (y0 == y1){
			if (x0 < x1){
				index = (y0) * Constants.GRID_WIDTH + y0 * (Constants.GRID_WIDTH+1) + x0;
			} else {
				index = (y0) * Constants.GRID_WIDTH + y0 * (Constants.GRID_WIDTH+1) + x1;
			}

		}
		return index;
	}

	@Override
	public boolean equals(Object obj){
		if (obj instanceof Edge){
			Edge e = (Edge) obj;
			if (e.getPos0() != null && e.getPos1() != null && this.getPos0() != null && this.getPos0() != null){
				if (e.getPos0().equals(this.getPos0()) && e.getPos1().equals(this.getPos1())){
					return true;
				}
				if (e.getPos1().equals(this.getPos0()) && e.getPos0().equals(this.getPos1())){
					return true;
				}
			}
		}
		return false;
	}
}
